package com.srwl.mytx.widget;

import android.content.Context;
import android.view.View;
import android.widget.ImageView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import androidx.viewpager.widget.ViewPager;

import com.google.android.material.bottomsheet.BottomSheetDialog;
import com.srwl.mytx.R;
import com.srwl.mytx.adapter.ConsumeSortAdapter;

import java.util.List;


public class ConsumeSortDialog extends BottomSheetDialog {
    private Context context;
    private List<String> consumeTypes;
    private ConsumeSortAdapter consumeSortAdapter;
    private ViewPager viewPager;
    private List<ImageView> dots; //小圆点2

    public ConsumeSortDialog(@NonNull Context context) {
        super(context);
    }

    public ConsumeSortDialog(@NonNull Context context, List<String> consumeTypes, int selectItem) {
        super(context, R.style.BottomDialog);
        setContentView(R.layout.consume_type_dialog);
        this.consumeTypes = consumeTypes;
        this.context = context;
        init(consumeTypes, selectItem);
    }


    private void init(List<String> consumeTypes, int selectItem) {
        if (consumeTypes == null) return;

        RecyclerView mRecyclerView = findViewById(R.id.vp);
        mRecyclerView.setLayoutManager(new GridLayoutManager(context, 3));
        consumeSortAdapter = new ConsumeSortAdapter(context, consumeTypes, selectItem);
        mRecyclerView.setAdapter(consumeSortAdapter);

        /*int count = consumeTypes.size();     //消费种类数量
        int pageCount;
        int pageSize = 9;
        int m = count % pageSize;
        if (m != 0) {
            pageCount = count / pageSize + 1;
        } else {
            pageCount = count / pageSize;
        }
        List<RecyclerView> list = new ArrayList<>();
        for (int i = 1; i <= pageCount; i++) {
            RecyclerView mRecyclerView = new RecyclerView(context);
            mRecyclerView.setLayoutManager(new GridLayoutManager(context, 3));
            if (m == 0) {   //刚好每页填满
                List<String> datas = consumeTypes.subList((i - 1) * pageSize, pageSize * i);
                consumeSortAdapter = new ConsumeSortAdapter(context, datas);
                mRecyclerView.setAdapter(consumeSortAdapter);
                list.add(mRecyclerView);
            } else {     //最后一页有空缺
                if (i == pageCount) {  //最后一页
                    List<String> datas = consumeTypes.subList((i - 1) * pageSize, count);
                    consumeSortAdapter = new ConsumeSortAdapter(context, datas);
                    mRecyclerView.setAdapter(consumeSortAdapter);
                    list.add(mRecyclerView);
                } else {
                    List<String> datas = consumeTypes.subList((i - 1) * pageSize, pageSize * i);
                    consumeSortAdapter = new ConsumeSortAdapter(context, datas);
                    mRecyclerView.setAdapter(consumeSortAdapter);
                    list.add(mRecyclerView);
                }
            }
        }


        viewPager = findViewById(R.id.vp);
        viewPager.setAdapter(new PresentAdapter(context, list));
        LinearLayout dotLayout = findViewById(R.id.dot);
        dots = new ArrayList<>();
        for (int i = 1; i <= pageCount; i++) {
            ImageView dot = new ImageView(context);
            dot.setImageResource(R.drawable.checked02);
            dot.setMaxWidth(10);
            dot.setMaxHeight(10);
            dots.add(dot);
            dotLayout.addView(dot);
        }
        //将第一个小圆点设为默认选中
        (dots.get(0)).setImageResource(R.drawable.checked01);


        //滑动监听
        viewPager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int i, float v, int i1) {
            }

            @Override
            public void onPageSelected(int pos) {
                for (int i = 0; i < dots.size(); i++) {
                    ImageView dot = dots.get(i);
                    if (pos == i) {
                        dot.setImageResource(R.drawable.checked01);
                    } else {
                        dot.setImageResource(R.drawable.checked02);
                    }
                }
            }

            @Override
            public void onPageScrollStateChanged(int i) {

            }
        });*/
    }

    public void setOnItemClickListener(ConsumeSortAdapter.OnItemClickListener itemClickListener) {
        consumeSortAdapter.setOnItemClickListener(itemClickListener);
    }


}
